import { forwardRef, useRef } from "react";

const Demofunction = forwardRef((props, ref) => {
    const instanceMethod = () => {
        console.log("我是函数式组件的方法");
    }
    ref.current = {instanceMethod};
    return (
        <div>我是组件实例</div>
    )
})

const ParentComponent = () => {
    const componnentRef = useRef(null);
    function handleClick()
    {
        if (componnentRef.current?.instanceMethod)
        {
            componnentRef.current.instanceMethod();
        }    
    }

    return (
        <div>
            <Demofunction ref={componnentRef}></Demofunction>
            <button onClick={()=>handleClick()}>按钮</button>
        </div>
    )
}


export default ParentComponent;